// page/component/userInfoEdit/index.js
const {request} = require('../../../utils/request');
const app = getApp();

Page({
  data: {
    userInfo: {}
  },

  onLoad() {
    this.setData({
      userInfo: app.globalData.userInfo
    });
  },

  onPhoneInput(e) {
    this.setData({
      'userInfo.phone': e.detail.value
    });
  },
  // 修正邮箱输入方法的拼写错误
  onEmailInput(e) {
    this.setData({
      'userInfo.email': e.detail.value
    });
  },

  onNicknameInput(e) {
    this.setData({
      'userInfo.nickname': e.detail.value
    });
  },

  async submitEditUserInfo() {
    // 打印发送的数据
    console.log('Data to be sent:', this.data.userInfo); 
    request({
      url: "/user",
      method: 'PUT',
      data: this.data.userInfo,
      showLoading: true
    })
    .then((res) => {
      if (res.code === 2000) {
        wx.showToast({
          title: '修改成功',
          icon: 'success'
        });
        app.globalData.userInfo = this.data.userInfo;
        wx.navigateBack();
      } else {
        wx.showToast({
          title: '修改失败',
          icon: 'none'
        });
        console.error('修改失败:', res.message);
      }
    })
    .catch((error) => {
      wx.showToast({
        title: '请求出错',
        icon: 'none'
      });
      console.error('修改用户请求出错:', error);
    });
  }
})
